package com.blackitheima.test1;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

//遍历集合
public class MyHashMap3 {
    public static void main(String[] args) {
        //创建集合
        HashMap<String,String> hm=new HashMap<>();
        hm.put("高山流水","知音难觅");
        hm.put("画饼充饥","俗不可耐");
        hm.put("激流勇进","飞流直下");

        //提取键值,并遍历
        Set<String> keys = hm.keySet();
        for (String key : keys) {
            String value = hm.get(key);
            System.out.println(key+"----"+value);
        }

        System.out.println("==================");

        //提取键值对,并遍历
        Set<Map.Entry<String, String>> entries = hm.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"----"+value);
        }

        System.out.println("==================");
        //forEach 方法
        hm.forEach(
                (String key,String value)->{
                    System.out.println(key+"----"+value);
                }
        );
    }
}
